package com.baoyi.pooke;

import net.youmi.android.AdManager;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.Toast;

import com.baoyi.adapter.BaoyiBaseAdapter;
import com.baoyi.content.content;

public abstract class BaoyiGallery extends Activity {
	protected static final int ITEM1 = 1;
	protected BaoyiBaseAdapter adapter;
	protected int selectIndex;
	protected int oldselectIndex = 0;
	protected int times = 0;
	protected boolean isrun = false;
	protected String fileitem;
	protected Gallery gallery;
	protected ImageButton pre;
	protected ImageButton up;

	static {
		AdManager.init(content.id, content.pwd, content.time, content.con,
				content.banben);
	}

	public BaoyiGallery() {
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.message);

		/** 全屏设置，隐藏窗口所有装饰 */
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		fileitem = "";
		oninitApp();
		initGallery();
		pre = (ImageButton) findViewById(R.id.imageviewpre);
		up = (ImageButton) findViewById(R.id.imageviewup);
		up.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// selectIndex;
				int path = selectIndex - 1;
				if (path >= 0) {
					gallery.setSelection(path);
				}

			}
		});
		pre.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// selectIndex;
				int path = selectIndex + 1;
				if (path < adapter.getCount()) {
					gallery.setSelection(path);
				}

			}
		});
		pre.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				hiddenImgbutton();
				return false;
			}
		});
		up.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				hiddenImgbutton();
				return false;
			}
		});
		// this.openOptionsMenu();
		watchWork();
	}

	/**
	 * 显示导航切换
	 */
	private void showImgbutton() {
		if (!isrun) {
			watchWork();
		}
		pre.setVisibility(View.VISIBLE);
		up.setVisibility(View.VISIBLE);
	}

	/**
	 * 显示导航切换
	 */
	private void hiddenImgbutton() {
		pre.setVisibility(View.GONE);
		up.setVisibility(View.GONE);
		Log.d(BaoyiApplication.TAG, "影藏按钮");
	}

	public void watchWork() {
		isrun = true;
		final Handler handler = new Handler();
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				// 要做的事情
				times++;
				if (times > 3) {
					hiddenImgbutton();
					times = 0;
					isrun = false;
					closeOptionsMenu();
				} else {
					handler.postDelayed(this, 1000);
				}
			}
		};
		handler.postDelayed(runnable, 1000);
	}

	/**
	 * 初始化幻灯片
	 */
	private void initGallery() {
		gallery = (Gallery) findViewById(R.id.gallery);

		initAdapter();
		gallery.setAdapter(adapter);
		gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				selectIndex = position;// 记录被选中的图片索引
				if (selectIndex == (adapter.getCount() - 1)) {
					Toast.makeText(BaoyiGallery.this, "已到最后一张图片",
							Toast.LENGTH_SHORT).show();
				}
				times = 0;
			}

			public void onNothingSelected(AdapterView<?> parent) {

			}
		});

		gallery.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				BaoyiGallery.this.openOptionsMenu();
				showImgbutton();
				times = 0;
				return false;
			}
		});
	}

	public void toggle() {
		if (up.getVisibility() == View.GONE) {
			showImgbutton();
		} else {
			hiddenImgbutton();
		}
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, 2, 0, "顶一下");
		menu.add(0, ITEM1, 0, "设置壁纸");
		menu.add(0, 3, 0, "返回");
		showImgbutton();
		return true;
	}

	@Override
	public void onOptionsMenuClosed(Menu menu) {
		// TODO Auto-generated method stub
		super.onOptionsMenuClosed(menu);
		// hiddenImgbutton();
	}

	protected abstract void setwallpaperManager();

	protected abstract void initAdapter();

	protected abstract void oninitApp();

	/**
	 * 菜单的方式设置壁纸
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		// 设置壁纸
		if (item.getItemId() == 1) {
			setwallpaperManager();
		}
		// 显示顶以后的消息
		if (item.getItemId() == 2) {

		}
		// 返回到首页
		if (item.getItemId() == 3) {
			BaoyiGallery.this.setResult(RESULT_OK, getIntent());
			BaoyiGallery.this.finish();
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}
}
